﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using mscook.mvc.Filters;
using mscook.mvc.Models;
using mscook.core.services;
using mscook.mvc.Core;
using mscook.core.domain;
using System.Security.Principal;
using mscook.mvc.core;

namespace mscook.mvc.Controllers
{
    public class BulletinController : BaseController
    {
        //
        // GET: /Bulletin/
         private ILoginUserService iLoginUserService;
         private IBulletinService iBulletinService;

         public BulletinController( IBulletinService iBulletinService,IActionBonusSettingService actionBonusSettingService, IResourceBonusSettingService resourceBonusSettingService, ILoginUserService loginUserService)
            : base(actionBonusSettingService, resourceBonusSettingService, loginUserService)
        {
            this.iLoginUserService = loginUserService;
            this.iBulletinService = iBulletinService;
        }
        public ActionResult Index(string num="")
        {

            BulletinModel bulletinModel = new BulletinModel();

            bulletinModel.ProfessionBulletins = iBulletinService.GetByType("厨艺培训", 0, 6);
            bulletinModel.CookBulletins = iBulletinService.GetByType("厨艺大赛", 0, 6);
            List<string> exitIds=new List<string>();
            exitIds.AddRange(bulletinModel.ProfessionBulletins.Select(o=>o.Id).ToList());
            exitIds.AddRange(bulletinModel.CookBulletins.Select(o => o.Id).ToList());

            long total = 0;
            PageBreakHelper pageBreakHelper = new PageBreakHelper();
            pageBreakHelper.Init(num, "10");

            bulletinModel.OtherBulletinList = iBulletinService.GetByExit(exitIds,pageBreakHelper.Start, pageBreakHelper.PerValue, out total).Where(o =>(!bulletinModel.ProfessionBulletins.Contains(o))&&(!bulletinModel.CookBulletins.Contains(o))).ToList();
            pageBreakHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();
            return View(bulletinModel);
        }

        public ActionResult ReadBulletin(string id)
        {
            Bulletin bulletin = iBulletinService.Get(id);
            if (bulletin != null)
            {
                ReadBulletinModel readModel = new ReadBulletinModel() { LoginUser = CurrentUser, Magazine = GetPushMagazine(), Bulletin = bulletin };

                return View(readModel);
            }
            return RedirectToAction("Index");
        }

    }
}
